package com.isecommerce.framework.util;

import java.util.Properties;
import java.io.File;
import java.io.FileInputStream;

/**
 * Config Manager<br><br>
 *
 * This class will read 'conf.properties'.
 *
 * @version 1.2.5
 * @author Leslie Chen
 */

public class Config {

    private Properties prop;
    private static Properties staticProp;
    private static String configFilePath;

    /**
     * Default constructor.
     */
    public Config() {
        super();
    }    

    /**
     * Get Properties obeject from config manager.
     *
     * @return Properties object.
     */
    public Properties getProperties() {
        return prop;
    }

    /**
     * Get value with given key name. (Static)
     *
     * @return Value string.
     * @param key Given key name.
     */
    public static String getStaticValue(String key) {
        return (String)staticProp.get(key);
    }

    /**
     * Get value with given key name
     *
     * @return Value string.
     * @param key Given key name.
     */
    public String getValue(String key) {
        if( prop==null ) return null;
        return (String)prop.get(key);
    }

    /**
     * Initializing Config Manager with given config file.
     *
     * @param configFile Filename with fullpath.
     */
    public void csInit(String configFile) {
        prop = new Properties();
        ClassLoader cl = this.getClass().getClassLoader();

        try {            
            prop.load(cl.getResourceAsStream(configFile));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * Initializing Config Manager with given config file.
     *
     * @param configFile Filename with fullpath.
     */
    public void webInit(String configFile) {
        prop = new Properties();
        ClassLoader cl = this.getClass().getClassLoader();

        try {
            prop.load( new FileInputStream(new File(configFile)));            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Static initializing method. (WEB)
     *
     * @param filename Filename with fullpath.
     */
    public static void staticWebInit(String filename) {
        System.out.println("Config: staticInit...");
        configFilePath = filename;

        try {
            staticProp = new Properties();
            staticProp.load( new FileInputStream(new File(configFilePath)));
        } catch (Exception e) {
            System.out.println("Config: staticInit ERROR->"+configFilePath);
            e.printStackTrace();
        }
    }

    /**
     * Static initializing method. (C/S)
     *
     * @param filename Filename with fullpath.
     */
    public static void staticCSInit(String filename) {
        System.out.println("Config: staticInit...");
        configFilePath = filename;

        ClassLoader cl = new Config().getClass().getClassLoader();

        try {
            staticProp = new Properties();
            staticProp.load(cl.getResourceAsStream(configFilePath));
        } catch (Exception e) {
            System.out.println("Config: staticInit ERROR->"+configFilePath);
            e.printStackTrace();
        }
    }

}
